<template>
  <div>
    <el-dialog title="选择测评" :visible.sync="isAddMeasurementPlan" width="60%" :before-close="handleClose" :close-on-click-modal='false'>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane :label="'平台推荐组合（'+this.numList.buildSelf+'）'" name="recommended">
          <recommended v-if="activeName === 'recommended'" :recommendedList="this.recommendedList" @haveChosen="haveChosen"></recommended>
        </el-tab-pane>
        <el-tab-pane :label="'自建测评组合（'+this.numList.recommend+'）'" name="selfBuilt">
          <selfBuilt v-if="activeName === 'selfBuilt'" :selfBuiltList="this.selfBuiltList" @haveChosen="haveChosen"></selfBuilt>
        </el-tab-pane>
        <el-tab-pane :label="'单个测评量表（'+this.numList.personal+'）'" name="single">
          <single v-if="activeName === 'single'" :singleList="this.singleList" @haveChosen="haveChosen"></single>
        </el-tab-pane>
      </el-tabs>
      <div class="distribution"><span class="distributionTitle">已选中</span><span class="distributionPrompt">共{{this.select.totalNumber}}个量表，已选中{{this.select.selected}}个，已选{{this.select.num}}题，预计{{this.select.time}}分钟完成</span></div>
      <simple-table :tableConfig="tableConfig" :tableData="selectTableData" @haveChosen="haveChosen"></simple-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="check()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import simpleTable from '@/components/simpleTable.vue'
import recommended from './recommended.vue'
import selfBuilt from './selfBuilt.vue'
import single from './single.vue'
import {APINUMBERCOMBINATIONS} from '@/common/api.js'
export default {
  props: ['isAddMeasurementPlan', 'plan_type', 'list'],
  components: {
    simpleTable,
    recommended,
    selfBuilt,
    single
  },
  data () {
    return {
      numList: {
        recommend: 0,
        buildSelf: 0,
        personal: 0
      },
      activeName: 'recommended',
      tableConfig: {
        load: false,
        size: 'small', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        border: true,
        config: [
          {name: '序号', type: 'index', width: '60px'},
          {id: 'student_title', name: '名称'},
          {id: 'topics_num', name: '题量'},
          {id: 'count', name: '时长'}
        ]
      },
      selectTableData: [],
      select: {
        totalNumber: 0,
        selected: 0,
        num: 0,
        time: 0
      },
      recommendedList: [],
      selfBuiltList: [],
      singleList: []
    }
  },
  created () {
    this.numListFn()
    if (this.plan_type === 1) {
      this.activeName = 'recommended'
      this.recommendedList = this.list
    } else if (this.plan_type === 2) {
      this.activeName = 'selfBuilt'
      this.selfBuiltList = this.list
    } else if (this.plan_type === 3) {
      this.activeName = 'single'
      this.singleList = this.list
    }
  },
  methods: {
    numListFn () {
      APINUMBERCOMBINATIONS(
      ).then((res) => {
        this.numList = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    haveChosen (val, totalNumber) {
      this.select.totalNumber = totalNumber
      this.selectTableData = val
      this.select.selected = this.selectTableData.length
      this.select.num = 0
      this.select.time = 0
      this.selectTableData.forEach(item => {
        this.select.num += Number(item.topics_num)
        this.select.time += Number(item.count)
      })
    },
    handleClick () {
      this.selectTableData = []
      this.select = {}
    },
    handleClose () {
      this.$emit('noAddMeasurementPlan', this.list)
    },
    check () {
      if (this.selectTableData.length <= 0) {
        this.$message.warning('请选择量表')
        return false
      }
      this.close(this.selectTableData)
    },
    close (val) {
      let id = Number
      if (this.activeName === 'recommended') {
        id = 1
      } else if (this.activeName === 'selfBuilt') {
        id = 2
      } else if (this.activeName === 'single') {
        id = 3
      }
      if (!val) {
        val = this.list
      }
      this.$emit('noAddMeasurementPlan', val, id)
    }
  }
}
</script>
<style lang="scss">
@import 'src/assets/style/varibles.scss';
.distribution{
  height: 40px;
  line-height: 40px;
  margin-top: 20px;
}
.distributionPrompt{
  color: $secondaryFontColor;
  font-size: $promptSize;
}
.distributionTitle{
  color: $mainFontColor;
  font-size: $secondarySize;
  display: inline-block;
  padding-right: 10px;
}
</style>
